import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'dart:async';
import 'package:myapp/Common/Remote/remote.dart';

class RemoteBloc extends Bloc<RemoteEvent, RemoteState> {

  final RemoteResponsitory responsity; //service

  RemoteBloc({@required this.responsity});

  bool isExecuting() {
    return _isExecuting;
  }

  bool _isExecuting = false;


  @override
  RemoteState get initialState => RemoteIntial();

  @override
  Stream<RemoteState> mapEventToState(RemoteEvent event) async* {
    if (event is RemoteFallBack) {
      yield  RemoteIntial();
    }

    if (!_isExecuting) { _isExecuting = true; }
    yield RemoteSendingToBackend();

    final result =  await responsity.execute(event.vin, event.toString());
    if ((result as RemoteExecuteResult).isVaild()) {
      final evenId = (result as RemoteExecuteResult).executionStatus.eventId;
      print("evenId: ${evenId}");
      print(result.toString());
      print("test111");
      yield RemoteSendingToBackendSuccess();
    } else {
      print("test22");
      yield RemoteSendingToBackendError();
    }
  }

//  @override
//  Stream<RemoteState> mapEventToState(RemoteEvent event) {
//
//    if (event is RemoteFallBack) {
//      return Stream<RemoteState>.value(RemoteIntial());
//    }
//    if (_isExecuting) {
//
//    }
//
//    final res =  responsity.execute(event.vin, event.toString());
//    return res.map((result){
//      if ((result as RemoteExecuteResult).isVaild()) {
//        final evenId = (result as RemoteExecuteResult).executionStatus.eventId;
//        print("evenId: ${evenId}");
//        print(result.toString());
//        return RemoteSendingToBackendSuccess();
//      } else {
//        return RemoteSendingToBackendError();
//      }
//    });
//  }
}